package com.umxwe.kill.common;

import lombok.Data;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Data

public class Main {
    private static Lock lock = new ReentrantLock();
    private static int state = 0;
    private static final Integer RUN_NUMBER = 10;

    static class ThreadA extends Thread{
        @Override
        public void run() {
            for(int i=0; i< RUN_NUMBER;){
                lock.lock();
                if(state %4 == 0){
                    System.out.println("第 "+(i+1)+"次" + "  A" );
                    state++;
                    i++;
                }
                lock.unlock();
            }
        }
    }

    static class ThreadB extends Thread{
        @Override
        public void run() {
            for(int i=0; i< RUN_NUMBER;){
                lock.lock();
                if(state %4 == 1){
                    System.out.println("第 "+(i+1)+"次" + "  B" );
                    state++;
                    i++;
                }
                lock.unlock();
            }
        }
    }

    static class ThreadC extends Thread{
        @Override
        public void run() {
            for(int i=0; i< RUN_NUMBER;){
                lock.lock();
                if(state %4 == 2){
                    System.out.println("第 "+(i+1)+"次" + "  C" );
                    state++;
                    i++;
                }
                lock.unlock();
            }
        }
    }

    static class ThreadD extends Thread{
        @Override
        public void run() {
            for(int i=0; i< RUN_NUMBER;){
                lock.lock();
                if(state %4 == 3){
                    System.out.println("第 "+(i+1)+"次" + "  D" );
                    state++;
                    i++;
                }
                lock.unlock();
            }
        }
    }
    public static void main(String[] args) {
        new ThreadA().start();
        new ThreadB().start();
        new ThreadC().start();
        new ThreadD().start();


    }

}


